Array.prototype.remove = function(b) {
    var a = this.indexOf(b);
    if (a >= 0) {
        this.splice(a, 1);
        return true;
    }
    return false;
};

var myLayout;
jQuery(document).ready(function ($) {
    myLayout = $('body').layout({
        north: {
            closable: false,
            resizable: false,
            spacing_open: 0
        },
        west: {
            closable: false,
            spacing_open: 5
        },
        south: {
            spacing_open: 5,
            minSize: 100
        }
    });
    $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
    //右键菜单效果
    $("ul.contextmenu-list li").bind("mouseover",function () {
        $(this).addClass('contextmenu-hover');
    }).bind("mouseout", function () {
            $(this).removeClass('contextmenu-hover');
        });

});

angular.module('Cvirt', ['interval', 'ui.calendar', 'ui.date', 'ui.bootstrap', 'CvirtDirectives', 'CvirtServices', 'CvirtFilters', 'ui.bootstrap.dialog', 'ngResource',
        '$strap.directives', 'ngCookies','angularFileUpload']).
    config(['$routeProvider', function ($routeProvider) {
        $routeProvider.
            when('/datacenter/:id', {templateUrl: '/static/partials/datacenters/index.html', controller: DataCenterCtrl}).
            when('/cluster/:id', {templateUrl: '/static/partials/clusters/index.html', controller: ClusterCtrl}).
            when('/host/:id', {templateUrl: '/static/partials/hosts/index.html', controller: HostCtrl}).
            when('/vm/:id', {templateUrl: '/static/partials/vms/index.html', controller: VMCtrl}).
            when('/templates', {templateUrl: '/static/partials/templates/index.html', controller: TemplateCtrl}).
            when('/networks', {templateUrl: '/static/partials/networks/index.html', controller: NetworkCtrl}).
//      when('/architects', {templateUrl: '/static/partials/architects/index.html', controller: ArchitectCtrl}).
            when('/shortcuts', {templateUrl: '/static/partials/shortcuts/index.html', controller: ShortcutCtrl}).
            when('/proposals', {templateUrl: '/static/partials/proposals/index.html'}).
            when('/backupstrategies', {templateUrl: '/static/partials/backupstrategies/index.html', controller: BackupStrategyCtrl}).
            when('/globalconfigs', {templateUrl: '/static/partials/globalconfigs/index.html', controller: GlobalConfigCtrl}).
            when('/platform', {templateUrl: '/static/partials/platform/index.html', controller: PlatformCtrl}).
            when('/users', {templateUrl: '/static/partials/user/index.html', controller: UserCtrl}).
            when('/', {templateUrl: '/static/partials/index.html', controller: GuideController}).
            otherwise({redirectTo: '/'});
    }])
    .run(function ($pollingPool, $timeout, $location,$rootScope, Activity,VM,$filter,Host) {
        //登录页面除外
        var ll = $location.absUrl()
        if(ll.indexOf('/login.html')==-1){

            console.log("start activity jobs")
            $pollingPool.add(function (callbacks) {
                if($rootScope.runactivity){
                    Activity.list({}, function(data){
                        $rootScope.activities = data;
                        angular.forEach(data,function(a){
                            if(a.meta.status=='SUCCESS'){
                                var c = callbacks[a.meta.task_id];
                                if(c){
                                    console.log('execute callback '+c);
                                    c(a.meta.status);
                                    callbacks[a.meta.task_id] = null;
                                }
                            }
                        });
                    },angular.noop);
                }
            },1000,true);

            $rootScope.$on('$routeChangeStart', function (e, route) {
                $pollingPool.clear();
            });

            //更新树结构虚拟机状态图标
            $pollingPool.add(function(){

                var tree = $.fn.zTree.getZTreeObj('search-tree');
                var vmids = tree.getNodesByParam('type','vm').map(function(item){
                    return item.did;
                })
                var ids = vmids.join(',');
                VM.status({ids:ids},function(data){
                    angular.forEach(data,function(vm){
                        var node = tree.getNodesByFilter(function(node){
                            return node.did == vm.id && node.type == 'vm';
                        },true);
                        if(node){
                            node.icon = "/static/images/vmstatus/"+$filter('lowercase')(vm.status)+".png"
                            tree.updateNode(node);
                        }
                    })
                })
            },5000,true);

            //更新主机状态,虚拟机和主机图标都依赖主机状态
            $pollingPool.add(function(){
                Host.status({}, angular.noop);
            },5000,true);
        }
    });



OSTYPES=[{'desc': 'Arch Linux', 'name': 'ArchLinux'},
    {'desc': 'Arch Linux (64 bit)', 'name': 'ArchLinux_64'},
//    {'desc': 'DOS', 'name': 'DOS'},
    {'desc': 'Debian', 'name': 'Debian'},
    {'desc': 'Debian (64 bit)', 'name': 'Debian_64'},
    {'desc': 'Fedora', 'name': 'Fedora'},
    {'desc': 'Fedora (64 bit)', 'name': 'Fedora_64'},
    {'desc': 'FreeBSD', 'name': 'FreeBSD'},
    {'desc': 'FreeBSD (64 bit)', 'name': 'FreeBSD_64'},
    {'desc': 'Gentoo', 'name': 'Gentoo'},
    {'desc': 'Gentoo (64 bit)', 'name': 'Gentoo_64'},
//    {'desc': 'JRockitVE', 'name': 'JRockitVE'},
//    {'desc': 'L4', 'name': 'L4'},
    {'desc': 'Linux 2.2', 'name': 'Linux22'},
    {'desc': 'Linux 2.4', 'name': 'Linux24'},
    {'desc': 'Linux 2.4 (64 bit)', 'name': 'Linux24_64'},
    {'desc': 'Linux 2.6', 'name': 'Linux26'},
    {'desc': 'Linux 2.6 (64 bit)', 'name': 'Linux26_64'},
    {'desc': 'Mac OS X Server', 'name': 'MacOS'},
    {'desc': 'Mac OS X Server (64 bit)', 'name': 'MacOS_64'},
    {'desc': 'Mandriva', 'name': 'Mandriva'},
    {'desc': 'Mandriva (64 bit)', 'name': 'Mandriva_64'},
    {'desc': 'NetBSD', 'name': 'NetBSD'},
    {'desc': 'NetBSD (64 bit)', 'name': 'NetBSD_64'},
    {'desc': 'Netware', 'name': 'Netware'},
//    {'desc': 'OS/2 Warp 3', 'name': 'OS2Warp3'},
//    {'desc': 'OS/2 Warp 4', 'name': 'OS2Warp4'},
//    {'desc': 'OS/2 Warp 4.5', 'name': 'OS2Warp45'},
    {'desc': 'OpenBSD', 'name': 'OpenBSD'},
    {'desc': 'OpenBSD (64 bit)', 'name': 'OpenBSD_64'},
    {'desc': 'Oracle', 'name': 'Oracle'},
    {'desc': 'Oracle (64 bit)', 'name': 'Oracle_64'},
    {'desc': 'Oracle Solaris 10 10/09 and later', 'name': 'OpenSolaris'},
    {'desc': 'Oracle Solaris 10 10/09 and later (64 bit)',
        'name': 'OpenSolaris_64'},
    {'desc': 'Oracle Solaris 10 5/09 and earlier', 'name': 'Solaris'},
    {'desc': 'Oracle Solaris 10 5/09 and earlier (64 bit)', 'name': 'Solaris_64'},
    {'desc': 'Other Linux', 'name': 'Linux'},
//    {'desc': 'Other OS/2', 'name': 'OS2'},
    {'desc': 'Other Windows', 'name': 'WindowsNT'},
    {'desc': 'Other/Unknown', 'name': 'Other'},
//    {'desc': 'QNX', 'name': 'QNX'},
    {'desc': 'Red Hat', 'name': 'RedHat'},
    {'desc': 'Red Hat (64 bit)', 'name': 'RedHat_64'},
    {'desc': 'Turbolinux (64 bit)', 'name': 'Turbolinux'},
    {'desc': 'Ubuntu', 'name': 'Ubuntu'},
    {'desc': 'Ubuntu (64 bit)', 'name': 'Ubuntu_64'},
    {'desc': 'Windows 2000', 'name': 'Windows2000'},
    {'desc': 'Windows 2003', 'name': 'Windows2003'},
    {'desc': 'Windows 2003 (64 bit)', 'name': 'Windows2003_64'},
    {'desc': 'Windows 2008', 'name': 'Windows2008'},
    {'desc': 'Windows 2008 (64 bit)', 'name': 'Windows2008_64'},
    {'desc': 'Windows 3.1', 'name': 'Windows31'},
    {'desc': 'Windows 7', 'name': 'Windows7'},
    {'desc': 'Windows 7 (64 bit)', 'name': 'Windows7_64'},
    {'desc': 'Windows 8', 'name': 'Windows8'},
    {'desc': 'Windows 8 (64 bit)', 'name': 'Windows8_64'},
    {'desc': 'Windows 95', 'name': 'Windows95'},
    {'desc': 'Windows 98', 'name': 'Windows98'},
    {'desc': 'Windows Me', 'name': 'WindowsMe'},
    {'desc': 'Windows NT 4', 'name': 'WindowsNT4'},
    {'desc': 'Windows Vista', 'name': 'WindowsVista'},
    {'desc': 'Windows Vista (64 bit)', 'name': 'WindowsVista_64'},
    {'desc': 'Windows XP', 'name': 'WindowsXP'},
    {'desc': 'Windows XP (64 bit)', 'name': 'WindowsXP_64'},
    {'desc': 'Xandros', 'name': 'Xandros'},
    {'desc': 'Xandros (64 bit)', 'name': 'Xandros_64'},
//    {'desc': 'eComStation', 'name': 'OS2eCS'},
    {'desc': 'openSUSE', 'name': 'OpenSUSE'},
    {'desc': 'openSUSE (64 bit)', 'name': 'OpenSUSE_64'}]